<?php
namespace app\applet\model;
use Baidu\Baidu;
use think\Db;
use think\Model;
use think\Request;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/5
 * Time: 16:25
 */
class TenantManagement extends Model
{
    /**
     * 员工端租客列表
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function templateList(){
        $input=input();
        $page=isset($data['p']) ? trim($data['p']) : 1;
        $model=new \app\manage\model\UserRole();
        $create_id=$model->create_id($input['id']);
      //  var_dump($create_id);exit;
        $count= Db::name('tenant_management')
            ->alias('a')
            ->join('house_keeper b','a.tenant_user_id=b.id','Left')
            ->where(['a.create_id'=>$create_id])
            ->where(['a.tenant_agent'=>$input['id']])
            ->where(['a.tenant_state'=>1])
            ->count();
        $data= Db::name('tenant_management')
            ->alias('a')
            ->join('house_keeper b','a.tenant_user_id=b.id','Left')
            ->field('a.tenant_id,a.tenant_name,a.tenant_tel,b.icon,a.tenant_user_id')
            ->where(['a.create_id'=>$create_id])
            ->where(['a.tenant_agent'=>$input['id']])
            ->where(['a.tenant_state'=>1])
            ->page($page, 10)
            ->select();
      //  var_dump($data);exit;
        $nomore= Db::name('tenant_management')
            ->alias('a')
            ->join('house_keeper b','a.tenant_user_id=b.id','Left')
            ->field('a.tenant_id,a.tenant_name,a.tenant_tel,b.icon,a.tenant_user_id')
            ->where(['a.create_id'=>$create_id])
            ->where(['a.tenant_agent'=>$input['id']])
            ->where(['a.tenant_state'=>1])
            ->page($page+1, 10)
            ->select();
        foreach ($data as $k=>$v){
            //查询租客和合同关联表
            $user_id=Db::name('tenant_contract')
                ->where(['t_id'=>$v['tenant_id']])
                ->where(['t_type'=>1])
                ->select();
            $user_id_list=array_column($user_id,'t_c_mid');
            $id=implode(',',$user_id_list);
            //查找对应的合同
            $map['c_id'] = array('in',$id);
            $data[$k]['Signed']=Db::name('contract_management')
                ->where($map)
                ->where(['create_id'=>$create_id])
                ->where(['c_type'=>1])
                ->count();
            $data[$k]['will_expire']=Db::name('contract_management')
                ->where($map)
                ->where(['c_type'=>2])
                ->where(['create_id'=>$create_id])
                ->count();
            $data[$k]['expired']=Db::name('contract_management')
                ->where($map)
                ->where(['c_type'=>3])
                ->where(['create_id'=>$create_id])
                ->count();
            $data[$k]['overdue']=Db::name('contract_management')
                ->where($map)
                ->where(['c_type'=>4])
                ->where(['create_id'=>$create_id])
                ->count();
        }
        if($nomore==null){
            $nomore=true;
        }else{
            $nomore=false;
        }
        $i['no_more']=$nomore;
        $i['data']=$data;
        $i['count']=$count;
        return $i;
    }
    /**
     * 租客詳情
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tenantDetails(){
        $input=input();
        $page=isset($data['p']) ? trim($data['p']) : 1;
      /*  $model=new \app\manage\model\UserRole();
        $create_id= $model->create_id($input['id']);*/
        $data= Db::name('tenant_management')
            ->alias('a')
            ->join('house_keeper b','a.tenant_user_id=b.id','Left')
            ->field('a.tenant_id,a.tenant_name,a.tenant_tel,b.icon,a.tenant_user_id')
            ->where(['a.tenant_id'=>$input['tenant_id']])
         //   ->where(['a.create_id'=>$create_id])
           // ->where(['a.tenant_agent'=>$input['id']])
            ->where(['a.tenant_state'=>1])
            ->find();
        //查询租客和合同关联表
        $count=Db::name('tenant_contract')
            ->where(['t_id'=>$data['tenant_id']])
            ->where(['t_type'=>1])
            ->count();
        $user_id=Db::name('tenant_contract')
            ->where(['t_id'=>$data['tenant_id']])
            ->where(['t_type'=>1])
            ->page($page, 10)
            ->select();
        $nomore=Db::name('tenant_contract')
            ->where(['t_id'=>$data['tenant_id']])
            ->where(['t_type'=>1])
            ->page($page+1, 10)
            ->select();
        $user_id_list=array_column($user_id,'t_c_mid');
        $id=implode(',',$user_id_list);
        //查找对应的合同
        $map['c_id'] = array('in',$id);
        $management=Db::name('contract_management')
            ->field('c_id,c_number,c_a_time,c_d_time,c_type')
            ->where($map)
            ->order('c_id desc')
            ->select();
        //根据合同查找合同住房
        foreach( $management as $k=>$v){
            $management[$k]['c_a_time']=date('Y-m-d',$v['c_a_time']);
            $management[$k]['c_d_time']=date('Y-m-d',$v['c_d_time']);
            $management[$k]['housing']=Db::name('contracted_housing')
                ->field('c_number')
                ->where(['c_c_mid'=>$v['c_id']])
                ->select();
        }
        $data['management']=$management;
        if($nomore==null){
            $nomore=true;
        }else{
            $nomore=false;
        }
        $i['no_more']=$nomore;
        $i['data']=$data;
        $i['count']=$count;
        return $i;
    }



    /**
     * 添加的时候判断手机号是否重复
     * @param $phone
     * @return array|false|\PDOStatement|string|Model
     */
    public function tenantPhone($phone,$id){
        $where['tenant_tel'] = $phone;
        $model=new \app\manage\model\UserRole();
        $create_id= $model->create_id($id);
        $user=Db::name('tenant_management')
            ->where(['create_id'=>$create_id])
            ->where($where)
            ->find();
       // var_dump($where,$user);exit;
        return $user;
    }

    /**
     *租客添加
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tenantAdd($data){
        //添加操作用户id
        $model=new \app\manage\model\UserRole();
        $user=Db::name('house_keeper')->where(['tel'=>$data['tenant_tel']])->find();
        if($user!=null){
            $arr['tenant_user_id']=$user['id'];
        }
        $arr['tenant_front'] =  "https://" .$_SERVER['SERVER_NAME'].$data['tenant_front'];//正面
        $arr['tenant_back'] =  "https://" .$_SERVER['SERVER_NAME'].$data['tenant_back'];//反面
        $card=new Baidu();
        //获取正面信息
        $img=$arr['tenant_front'];//图片信息
        $card_side='front';//front:正面  back：反面
        $i=$card->card($img,$card_side);
        $i=json_decode($i);
        if($i->words_result->公民身份号码->words==null){
            return 2;
        }
        $tenant_back=$arr['tenant_back'];//图片信息
        $back='back';//front:正面  back：反面
        $code=$card->card($tenant_back,$back);
        $code=json_decode($code);
        if($code->words_result->签发机关->words==null){
            return 3;
        }
        $where=[];
        $where['tenant_idcard'] = $i->words_result->公民身份号码->words;
        $where['create_id'] = $model->create_id($data['tenant_agent']);
        $user=Db::name('tenant_management')
            ->where($where)
            ->find();
        if($user!=null){
            return 4;
        }
        $arr['tenant_front'] =  $data['tenant_front'];//正面
        $arr['tenant_back'] = $data['tenant_back'];//反面
        //处理数据
        $arr['tenant_name'] = $i->words_result->姓名->words;
        $arr['tenant_sex'] = tenant_sex($i->words_result->性别->words);
        $arr['tenant_tel'] = isset($data['tenant_tel']) ? trim($data['tenant_tel']) : "";

     //   $create_id=$model->create_id($input['id']);
        $arr['tenant_agent'] =  isset($data['tenant_agent']) ? trim($data['tenant_agent']) : "";
        $arr['create_id'] =  $model->create_id($data['tenant_agent']);
        $arr['tenant_idcard'] = $i->words_result->公民身份号码->words;
        $arr['tenant_famous_race'] = $i->words_result->民族->words;
        $arr['date_of_birth'] = $i->words_result->出生->words;
        $arr['tenant_issue'] = $code->words_result->签发机关->words;
        $arr['tenant_address'] = $i->words_result->住址->words;
        $arr['tenant_valid_date'] = $code->words_result->签发日期->words.' - '.$code->words_result->失效日期->words;
        $arr['tenant_a_time'] = time();
        $data=Db::name('tenant_management')->insertGetId($arr);
        if($data){
            $data= Db::name('tenant_management')->where(['tenant_id'=>$data])->find();
            return $data;
        }else{
            return false;
        }

    }

}